package com.example.casper.Experiment2024.utils;

import android.content.Context;
import android.util.Log;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.entity.BookInfo;

import java.util.ArrayList;
import java.util.List;

public class BookModel {

    private static FileUtils fileUtils = new FileUtils();

    public static void init(Context context) {
        List<BookInfo> data = new ArrayList<>();
        data.add(new BookInfo(R.drawable.book_no_name, "创新工程实践"));
        data.add(new BookInfo(R.drawable.book_2, "软件项目管理案例教程（第3版）"));
        data.add(new BookInfo(R.drawable.book_1, "信息安全教学基础（第2版）"));
        fileUtils.writeFile(context, "books", data);
    }

    public static List<BookInfo> getData(Context context) {
        List<BookInfo> books = (List<BookInfo>) fileUtils.readFile(context, "books");
        Log.e("TAG", "getData: " + books);
        return books;
    }

    public static void saveDataByPosition(Context context, String operate, String bookName, int position) {
        List<BookInfo> books = getData(context);
        if (operate.equals("add")) {
            BookInfo bookInfo = new BookInfo(R.drawable.book_no_name, bookName);
            books.add(position + 1, bookInfo);
        } else if (operate.equals("modify")) {
            BookInfo bookInfo = new BookInfo(books.get(position).getPhoto(), bookName);
            books.set(position, bookInfo);
        }
        fileUtils.writeFile(context, "books", books);
    }

    public static void saveData(Context context, List<BookInfo> books) {
        fileUtils.writeFile(context, "books", books);
    }
}